﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

using Ordinaire.Commerce.Data;
using Ordinaire.Social.Data;

namespace Ordinaire.Commerce.Purchasing.Data
{
    /// <summary>
    /// Use this to capture all purchases.
    /// </summary>
    [Table]
    public class Purchases : Invoices
    {
        #region Variables

        /// <summary>
        /// Storage for related records in table "PurchaseItems".
        /// </summary>
        protected EntitySet<PurchaseItems> purchaseItems = new EntitySet<PurchaseItems>();

        #endregion

        #region Property methods

        /// <summary>
        /// Get or set the entry unique identifier.
        /// </summary>
        [Column(IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
        public override int Id
        {
            get { return base.Id; }
            set { base.Id = value; }
        }

        /// <summary>
        /// Gets or sets the invoice serial number.
        /// </summary>
        [Column(DbType = "nvarchar(50) NULL", UpdateCheck = UpdateCheck.Never)]
        public override string SerialNo
        {
            get { return base.SerialNo; }
            set { base.SerialNo = value; }
        }

        /// <summary>
        /// Gets or sets the purchase date.
        /// </summary>
        [Column(DbType = "datetime NULL", UpdateCheck = UpdateCheck.Never)]
        public override DateTime Date
        {
            get { return base.Date; }
            set { base.Date = value; }
        }

        /// <summary>
        /// Gets or sets whom (as individual) this purchase belong to.
        /// </summary>
        [Column(DbType = "int NULL", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
        public override int? PersonId
        {
            get { return base.PersonId; }
            set { base.PersonId = value; }
        }

        /// <summary>
        /// Gets or sets whom (as organisation) this purchase belong to.
        /// </summary>
        [Column(DbType = "int NULL", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
        public override int? OrganisationId
        {
            get { return base.OrganisationId; }
            set { base.OrganisationId = value; }
        }

        /// <summary>
        /// Gets or sets the discount amount given for this purchase.
        /// </summary>
        [Column(DbType = "decimal(18,5) NOT NULL DEFAULT(0)", UpdateCheck = UpdateCheck.Never)]
        public override decimal Discount
        {
            get { return base.Discount; }
            set { base.Discount = value; }
        }

        /// <summary>
        /// Gets or sets the tax amount charged for this purchase.
        /// </summary>
        [Column(DbType = "decimal(18,5) NOT NULL DEFAULT(0)", UpdateCheck = UpdateCheck.Never)]
        public override decimal Tax
        {
            get { return base.Tax; }
            set { base.Tax = value; }
        }

        #region Overrides IAudit

        /// <summary>
        /// Get or set the username who created this entry.
        /// </summary>
        [Column(DbType = "nvarchar(10) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public override string CreatedBy
        {
            get { return base.CreatedBy; }
            set { base.CreatedBy = value; }
        }

        /// <summary>
        /// Get or set the date when the entry was first created.
        /// </summary>
        [Column(DbType = "datetime NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public override DateTime? CreateDate
        {
            get { return base.CreateDate; }
            set { base.CreateDate = value; }
        }

        /// <summary>
        /// Get or set the username who last updated this entry.
        /// </summary>
        [Column(DbType = "nvarchar(10) NULL", UpdateCheck = UpdateCheck.Never)]
        public override string UpdatedBy
        {
            get { return base.UpdatedBy; }
            set { base.UpdatedBy = value; }
        }

        /// <summary>
        /// Get or set the date when the entry was last updated.
        /// </summary>
        [Column(DbType = "datetime NULL", UpdateCheck = UpdateCheck.Never)]
        public override DateTime? UpdateDate
        {
            get { return base.UpdateDate; }
            set { base.UpdateDate = value; }
        }

        #endregion

        #region Association

        /// <summary>
        /// Gets or sets the associated data to table 'People'.
        /// </summary>
        [Association(Name = "FK_Purchases_People", IsForeignKey = true, ThisKey = "PersonId", OtherKey = "Id", Storage = "people")]
        public override People People
        {
            get { return base.People; }
            set { base.People = value; }
        }

        /// <summary>
        /// Gets or sets the associated data to table 'Organisations'.
        /// </summary>
        [Association(Name = "FK_Purchases_Organisations", IsForeignKey = true, ThisKey = "OrganisationId", OtherKey = "Id", Storage = "organisations")]
        public override Organisations Organisations
        {
            get { return base.Organisations; }
            set { base.Organisations = value; }
        }

        /// <summary>
        /// Gets or sets the associated data to table 'PurchaseGoods'.
        /// </summary>
        [Association(ThisKey = "Id", OtherKey = "ForeignId", Storage = "purchaseItems")]
        public EntitySet<PurchaseItems> PurchaseItems
        {
            get { return this.purchaseItems; }
        }

        #endregion

        #endregion
    }

    /// <summary>
    /// Contains list of extension methods for Ordinaire.Commerce.Data.Purchases.
    /// </summary>
    public static class PurchasesExtension
    {
        /// <summary>
        /// Converts purchase data into an instance of Ordinaire.Commerce.Invoice.
        /// </summary>
        /// <param name="data">the purchase data</param>
        /// <returns>Ordinaire.Commerce.Invoice instance representing the purchase data</returns>
        public static Purchase ToPurchase(this Purchases data)
        {
            Purchase purchase = data.ToInvoice<Purchase>();
            purchase.Details  = data.PurchaseItems.ToList().ToInvoiceDetails();
            return purchase;
        }
    }
}
